package com.acme.domain;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import com.acme.domain.request.Request;

public class DispatcherCommand implements Runnable{

	private Request request;
	private static Map<String,Stock> stockMap = new HashMap<String,Stock>();
	private static Logger dispatcherLogger = Logger.getLogger(DispatcherCommand.class.getName());
	
	public DispatcherCommand(Request recievedRequest) {
		this.request = recievedRequest;
	}

	@Override
	public void run() {
		
		String currentStockName = request.getStockName();
		
		if ( !stockMap.containsKey(currentStockName)){
			stockMap.put( currentStockName, new Stock(currentStockName));
			dispatcherLogger.info("added stock "+currentStockName);
		}
		
		Stock currentStock = stockMap.get(currentStockName);
		currentStock.execute(request);			
	}

	public static Map<String, Stock> getStockMap() {
		return stockMap;
	}
	
}
